﻿myApp.controller("ChildCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope) {
    var sort = $routeParams.sort;
    $scope.Childinfo = {
        name: '',
        birthday: '',
        choose_grade: {},
        school:''

    };
    $scope.Grades = [
        {
            time: new Date().getFullYear() - 1 + '-09-01',
            name: '小学一年级'
        },
        {
            time: new Date().getFullYear() - 2 + '-09-01',
            name: '小学二年级'
        },
        {
            time: new Date().getFullYear() - 3 + '-09-01',
            name: '小学三年级'
        },
        {
            time: new Date().getFullYear() - 4 + '-09-01',
            name: '小学四年级'
        },
        {
            time: new Date().getFullYear() - 5 + '-09-01',
            name: '小学五年级'
        },
        {
            time: new Date().getFullYear() - 6 + '-09-01',
            name: '初中一年级'
        },
        {
            time: new Date().getFullYear() - 7 + '-09-01',
            name: '初中二年级'
        },
        {
            time: new Date().getFullYear() - 8 + '-09-01',
            name: '初中三年级'
        }
    ]
    //获取用户绑定的孩子
    $scope.GetCustomerChilds = function () {
        $http({ method: "GET", url: $restUrls["getChildBySort"], params: {sort:sort} }).success(function (result) {
            if (result.success == 1) {
                $scope.child= result.data;
                if($scope.child.grade){
                    for(var i=0;i<$scope.Grades.length;i++){
                         if($scope.child.grade==$scope.Grades[i].time)
                         {
                             $scope.Childinfo.choose_grade =$scope.Grades[i];
                         }
                    }
                }
                $scope.Childinfo.name = $scope.child.name;
                $scope.Childinfo.birthday = $scope.child.birthday;
                $scope.Childinfo.school = $scope.child.school;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    $scope.GetCustomerChilds();
    var currYear = (new Date()).getFullYear();
    var opt = {};
    opt.date = { preset: 'date' };
    opt.datetime = { preset: 'datetime' };
    opt.time = { preset: 'time' };
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        // showNow: true,
        // nowText: "今天",
        startYear: currYear - 70, //开始年份
        endYear: currYear //结束年份
    };
    $("#appDate").mobiscroll($.extend(opt['date'], opt['default']));



    //选择年级
    $scope.ChooseGrade = function (grade) {
        $scope.gradeshow = false;
        $scope.Childinfo.choose_grade = grade;
    }


    //修改小孩
    $scope.sumbitone = false;
    $scope.ModifyChild = function (info) {
        if ($scope.sumbitone) {
            return;
        }
        if (!$scope.ChildForm.$valid) {
            $scope.showOneError = true;
            return;
        }
        var parameters = {
            name: info.name,
            birthday: info.birthday || '',
            grade: info.choose_grade.time || '',
            school:info.school,
            sort:sort
        };
        $scope.sumbitone = true;
        $post($restUrls["modifyChild"], parameters).success(function (result) {
            if (result.success == 1) {
                $scope.sumbitone = false;
                window.history.back();
            }
            else {
                $scope.sumbitone = false;
                $.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
            }
        }).error(function (data, status, headers, config) {
            $scope.sumbitone = false;
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        })
    }
}]);